﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Tool
{
  public  class IniCfg
    {

        public static string PathIN = AppDomain.CurrentDomain.BaseDirectory + "\\" + "Config" + "\\" + "IN.ini";//输入IO路径

        public static string PathOUT = AppDomain.CurrentDomain.BaseDirectory + "\\" + "Config" + "\\" + "OUT.ini";//输入IO路径

        public static Dictionary<string, int> IntIn = new Dictionary<string, int>();//输入IO

        public static Dictionary<string, int> IntOut = new Dictionary<string, int>();//输出IO

        public static Dictionary<string, int> ExtIn = new Dictionary<string, int>();//扩展0

        public static Dictionary<string, int> ExtOut = new Dictionary<string, int>();//扩展0

        #region 读取Ini数据到DataGridView控件中
        /// <summary>
        /// 读取Ini数据到DataGridView控件中
        /// </summary>
        /// <param name="dgv">控件</param>
        /// <param name="path">路径</param>
        /// <param name="Section">段落名称</param>
        public static void ReadIniToDGV(DataGridView dgv, string Path, string Section)
        {
            dgv.Rows.Clear();//清除集合

            if (File.Exists(Path))
            {
                //先得到所有Key键值
                string[] keys = Tool.IniFile.INIGetAllItemKeys(Path, Section);//获取所有的Key值
                //遍历所有Key键值
                foreach (string temp in keys)
                {
                    //得到所有的Value值
                    string value = Tool.IniFile.INIGetStringValue(Path, Section, temp, " ");//               
                    
                    object[] objects = new object[] { temp, value };//获取 Key键值 Value 
                    //添加到控件
                    dgv.Rows.Add(objects);

                }
            }
        }

        #endregion

        #region 读取Ini数据到系统中
        /// <summary>
        /// 读取Ini数据加载到系统中
        /// </summary>
        /// <param name="dic">字典集合</param>
        /// <param name="Path">路径</param>
        /// <param name="Section">段落名称</param>
        public static void WriteIniToSystem(Dictionary<string ,int>dic, string Path, string Section)
        {
            if (File.Exists(Path))
            {
                //先得到所有Key键值
                string[] keys = Tool.IniFile.INIGetAllItemKeys(Path, Section);//获取所有的Key值
                //遍历所有Key键值
                foreach (string temp in keys)
                {
                    //得到所有的Value值
                    string value = Tool.IniFile.INIGetStringValue(Path, Section, temp, " ");

                    dic.Add(value, Convert.ToInt16(temp));
                }
            }
            else
            {
                MessageBox.Show("未找到对应Ini配置文件");
            }
        }

        #endregion

        #region 创建新的CSV文件

        public static void CreateCsv(string string1 )
        {
            string path = SysConfig.Config["历史数据路径"].ToString();

            if (!Directory.Exists(path))//判断文件夹是否存在
            {
                System.IO.Directory.CreateDirectory(path);//判断路径是否存在,不存在则创建
            }         
            string Name = DateTime.Now.ToString("yyyy-MM-dd") +  "_生产数据.csv";
            string fileName = path + "\\" + Name;
            if (!File.Exists(fileName))//判断文件是否存在 不存在则创建
            {
                AddHead(fileName);//添加类标题行标题
            }
            Tool.CsvOperation csv = new Tool.CsvOperation();
            for (int i = 0; i < Tool.Count.TotalArray.Length; i++)
            {
                //csv[i, 0] = Tool.ChartShow.Time[i];
                //csv[i, 1] = Convert.ToString(Tool.Count.TotalArray[i]);
                //csv[i, 2] = Convert.ToString(Tool.Count.OKArray[i]);
                //csv[i, 3] = Convert.ToString(Tool.Count.NGArray[i]);              
            }
            csv.Save(fileName);

        }

        public static void AddHead(string fileName)
        {
            Tool.CsvOperation csv = new Tool.CsvOperation();
            csv[0, 0] = "Time";
            csv[0, 1] = "总数";
            csv[0, 2] = "OK数量";
            csv[0, 3] = "NG数量";
            csv.Save(fileName);
        }






        #endregion



    }
}
